<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/28
 * Time: 18:04
 */

namespace App\HttpController\Home;


use EasySwoole\HttpClient\HttpClient;

class DouyuLive extends Base
{
    protected $lol_cate_id = 1;
    protected $dota_cate_id = 3;
    protected $kog_cate_id = 181;
    protected $jdqs_cate_id = 270;
    protected $csgo_cate_id = 6;
    protected $live_url = 'http://open.douyucdn.cn/api/RoomApi/live';
    public $total = 100;
    /**
     * 全部直播
     * @return bool
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     */
    public function lists()
    {
        $page = $this->request()->getQueryParam('page') ? : 1;
        $pageSize = $this->request()->getQueryParam('pageSize') ? : 20;
        $offest = ( $page - 1 ) * $pageSize;
        $limit = $pageSize;
        $url = $this->live_url.'?offset='.$offest.'&limit='.$limit;
        $client = new HttpClient($url);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        $res = [
            'currentPage' => (int)$page,
            'total'       => $this->total,
            'data'        => $data['data']
        ];
        return $this->writeJson(1,$res,'请求成功');
    }

    /**
     * 直播分类
     * @return bool
     */
    public function cates()
    {
        return $this->writeJson(1,[
            'lol'   => $this->lol_cate_id,
            'dota'  => $this->dota_cate_id,
            'kog'   => $this->kog_cate_id,
            'csgo'  => $this->csgo_cate_id,
            'jdqs'  => $this->jdqs_cate_id
        ],'请求成功');
    }

    /**
     * lol直播
     * @return bool
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     */
    public function lol()
    {
        $page = $this->request()->getQueryParam('page') ? : 1;
        $pageSize = $this->request()->getQueryParam('pageSize') ? : 20;
        $offest = ( $page - 1 ) * $pageSize;
        $limit = $pageSize;
        $url = $this->live_url.'/'.$this->lol_cate_id.'?offset='.$offest.'&limit='.$limit;
        $client = new HttpClient($url);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        $res = [
            'currentPage' => (int)$page,
            'total'       => $this->total,
            'data'        => $data['data']
        ];
        return $this->writeJson(1,$res,'请求成功');
    }

    public function dota()
    {
        $page = $this->request()->getQueryParam('page') ? : 1;
        $pageSize = $this->request()->getQueryParam('pageSize') ? : 20;
        $offest = ( $page - 1 ) * $pageSize;
        $limit = $pageSize;
        $url = $this->live_url.'/'.$this->dota_cate_id.'?offset='.$offest.'&limit='.$limit;
        $client = new HttpClient($url);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        $res = [
            'currentPage' => (int)$page,
            'total'       => $this->total,
            'data'        => $data['data']
        ];
        return $this->writeJson(1,$res,'请求成功');
    }

    public function csgo()
    {
        $page = $this->request()->getQueryParam('page') ? : 1;
        $pageSize = $this->request()->getQueryParam('pageSize') ? : 20;
        $offest = ( $page - 1 ) * $pageSize;
        $limit = $pageSize;
        $url = $this->live_url.'/'.$this->csgo_cate_id.'?offset='.$offest.'&limit='.$limit;
        $client = new HttpClient($url);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        $res = [
            'currentPage' => (int)$page,
            'total'       => $this->total,
            'data'        => $data['data']
        ];
        return $this->writeJson(1,$res,'请求成功');
    }

    public function kog()
    {
        $page = $this->request()->getQueryParam('page') ? : 1;
        $pageSize = $this->request()->getQueryParam('pageSize') ? : 20;
        $offest = ( $page - 1 ) * $pageSize;
        $limit = $pageSize;
        $url = $this->live_url.'/'.$this->kog_cate_id.'?offset='.$offest.'&limit='.$limit;
        $client = new HttpClient($url);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        $res = [
            'currentPage' => (int)$page,
            'total'       => $this->total,
            'data'        => $data['data']
        ];
        return $this->writeJson(1,$res,'请求成功');
    }

    public function jdqs()
    {
        $page = $this->request()->getQueryParam('page') ? : 1;
        $pageSize = $this->request()->getQueryParam('pageSize') ? : 20;
        $offest = ( $page - 1 ) * $pageSize;
        $limit = $pageSize;
        $url = $this->live_url.'/'.$this->jdqs_cate_id.'?offset='.$offest.'&limit='.$limit;
        $client = new HttpClient($url);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        $res = [
            'currentPage' => (int)$page,
            'total'       => $this->total,
            'data'        => $data['data']
        ];
        return $this->writeJson(1,$res,'请求成功');
    }

    /**
     * 获取iframe的直播链接
     * @return bool
     */
    public function getVideoUrl()
    {
        $room_id = $this->request()->getQueryParam('room_id');
        if( !$room_id ){
            return $this->writeJson(0,null,'缺少room_id参数');;
        }
        $videoUrl = 'https://staticlive.douyucdn.cn/common/share/play.swf?room_id='.$room_id;
        return $this->writeJson(1,['url'=>$videoUrl],'请求成功');
    }
}